import request, { type ListQuery, type ListResponse } from "@/utils/request";

const MERCHANT_BASE_URL = "/api";

export const MerchantAPI = {
  getMerchantList: (params: MerchantListQuery) => {
    return request.get<any, ListResponse<MerchantType>>(`${MERCHANT_BASE_URL}/internal/merchant/`, {
      params,
    });
  },
  addMerchant: (data: Partial<MerchantForm>) => {
    return request.post(`${MERCHANT_BASE_URL}/internal/merchant/`, data, {
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  },
  updateMerchant: (data: Partial<MerchantForm>) => {
    return request.put(`${MERCHANT_BASE_URL}/internal/merchant/${data.id}/`, data, {
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  },
  deleteMerchant: (id: number) => {
    return request.delete(`${MERCHANT_BASE_URL}/internal/merchant/${id}/`);
  },
  examineMerchant: (data: any) => {
    return request.post(`${MERCHANT_BASE_URL}/merchtants/${data.id}/approve/`, data);
  },
};

export type MerchantListQuery = {} & ListQuery;

export type MerchantLocation = {
  id: number;
  name: string;
  full_name: string;
};

export type MerchantUserType = {
  id: number;
  username: string;
  avatar_url: string;
  nickname: string;
};

export type MerchantSubjectType = {
  created_at: string;
  id: number;
  image_url: string;
  is_active: boolean;
  name: string;
  sequence: number;
  updated_at: string;
};

export type MerchantAgentType = {
  id: number;
  user: MerchantUserType;
  province: MerchantLocation;
  city: MerchantLocation;
  district: MerchantLocation;
  created_at: string;
  updated_at: string;
  is_active: boolean;
  name: string;
};

/**
 * @description 商户状态
 * @enum {string}
 * @property {string} active 正常
 * @property {string} approve 审核中
 * @property {string} reject 审核拒绝
 * @property {string} deactive 未营业
 * @property {string} locked 锁定
 */
export type MerchantStatus = "active" | "approve" | "reject" | "deactive" | "locked";

export type MerchantType = {
  id: number;
  province: MerchantLocation;
  city: MerchantLocation;
  district: MerchantLocation;
  user: MerchantUserType;
  subjects: MerchantSubjectType[];
  agent?: MerchantAgentType;
  created_at: string;
  updated_at: string;
  is_active: boolean;
  name: string;
  address: string;
  longitude: string;
  latitude: string;
  status: MerchantStatus;
  contact_name: string;
  contact_phone: string;
  store_image: string;
  referrer: number;
};

export type MerchantForm = {
  id?: number;
  is_active: boolean;
  name: string;
  address: string;
  province: number;
  city: number;
  district: number;
  contact_name: string;
  contact_phone: string;
  store_image: string;
  referrer: number;
  user: number;
  agent?: number;
  action?: MerchantStatus;
  reason?: string;
};

export default MerchantAPI;
